#include "command.h"
#include "redix.h"

    Command::Command(Redix* rdx, long id, const std::string& cmd, 
	   const Callback& callback): m_rdx(rdx) , m_id(id) , m_cmd(cmd) , m_callback(callback) 
{
}

Command::~Command() {
}

void Command::free() {
    std::lock_guard<std::mutex> lock(rdx->m_freeMutex);
    rdx->m_freeQueue.push(m_id);
    ev_async_send(rdx->m_evLoop, &rdx->m_freeWatcher);
}

void Command::processReply(redisReply* r) {
    m_lastError.clear();
    m_reply = r;

    if (nullptr == m_reply) {
	Redix::disconnectCallback(m_rdx->ctx, REDIS_ERR);
    }
    else {
	std::lock_guard<std::mutex> lock(m_rdx->m_completedMutex);
	m_rdx->m_completedQueue.push(m_id);
    }
} 

void Command::freeReply() {
    if (nullptr == m_reply) {
	return;
    }

    freeReplyObject(m_reply);
    m_reply = nullptr;
}


